package net.maku.kyy.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import net.maku.kyy.query.KyyUserGameQuery;
import net.maku.kyy.service.KyyUserGameService;
import net.maku.kyy.vo.KyyGameVO;
import net.maku.kyy.vo.KyyUserGameVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;

/**
 * 用户的游戏表
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("/kyy/user/game")
@Tag(name="用户的游戏表")
@AllArgsConstructor
public class KyyUserGameController {
    private final KyyUserGameService kyyUserGameService;


    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('kyy:user')")
    public Result<PageResult<KyyUserGameVO>> page(@ParameterObject @Valid KyyUserGameQuery query){
        PageResult<KyyUserGameVO> page = kyyUserGameService.page(query);
        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('kyy:user')")
    public Result<KyyUserGameVO> get(@PathVariable("id") Long id){
        KyyUserGameVO data = kyyUserGameService.get(id);
        return Result.ok(data);
    }

    /**
     * 获取我的游戏
     * @return
     */
    @GetMapping("/my")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('kyy:user')")
    public Result<PageResult<KyyUserGameVO>> getMyGame(){
        PageResult<KyyUserGameVO> page = kyyUserGameService.getMyMap();
        return Result.ok(page);
    }

    /**
     * 获取我的游戏详细信息列表
     * @return
     */
    @GetMapping("/my/detail")
    @PreAuthorize("hasAuthority('kyy:user')")
    public Result<PageResult<KyyGameVO>> getMyGamePage(){
        PageResult<KyyGameVO> page = kyyUserGameService.getMyGamePage();
        return Result.ok(page);
    }


    /**
     * 保存我的游戏
     * @param vo
     * @return
     */
    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('kyy:user')")
    public Result<String> save(@RequestBody KyyUserGameVO vo){
        // 处理 URL 中的 HTML 实体
        if (vo.getKyyGameUrl() != null) {
            vo.setKyyGameUrl(vo.getKyyGameUrl().replace("&amp;", "&"));
        }
        kyyUserGameService.save(vo);
        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('kyy:user')")
    public Result<String> update(@RequestBody @Valid KyyUserGameVO vo){
        kyyUserGameService.update(vo);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('kyy:user')")
    public Result<String> delete(@RequestBody List<Long> idList){
        kyyUserGameService.delete(idList);

        return Result.ok();
    }


    @GetMapping("export")
    @Operation(summary = "导出")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('kyy:user')")
    public void export() {
        kyyUserGameService.export();
    }
}